package com.live555.sava264;

import com.live555.callback.IH264StoreFinishCallBack;
import com.live555.utils.FileTools;
import com.live555.utils.H264ToMp4Utils;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 功能描述：存储h264到本地
 *
 * @author：ZC 创建时间：2017/6/2
 * 最后修改日期：
 */
public class StoreH264FileRunnable implements Runnable {

    /**
     * 处理完毕回调
     */
    private IH264StoreFinishCallBack ih264StoreFinishCallBack;
    /**
     * 是否开启存储
     */
    private boolean isStoreH264File = false;
    private BlockingQueue<byte[]> mH264BytesQueue;
    /**
     * 存储路径
     */
    private String h264CurrentFilePath;


    public StoreH264FileRunnable() {
        mH264BytesQueue = new LinkedBlockingQueue<>();
    }

    @Override
    public void run() {
        //外部发送停止命令，并且队列数据存储完毕才能停止
        boolean isContinueStore = false;
        while (!isContinueStore) {
            isContinueStore = (!isStoreH264File) && isQueueEmpty();
            //保存数据
            FileTools.writeFileToSDCard(getFirstQueue(), h264CurrentFilePath);
//            LogUtils.i("StoreH264FileRunnable 存储中 ", "" + isQueueEmpty() + "  " + isStoreH264File + "  isContinueStore=" + isContinueStore);
        }
//        LogUtils.i("StoreH264FileRunnable", " 存储完毕------------------------");
        h264ToMp4Parse(h264CurrentFilePath);
        if (ih264StoreFinishCallBack != null) {
            ih264StoreFinishCallBack.onStoreAndParseFinished(true, "转化完成");
        }
//        LogUtils.i("StoreH264FileRunnable", "" + Thread.currentThread().getName());
    }//end run


    /**
     * H264转码成MP4
     */
    private boolean h264ToMp4Parse(String h264Path) {
//        LogUtils.i("开始转码  ", "" + Thread.currentThread().getName());
        if (h264Path == null) {
            return false;
        }

        String mp4Path = h264Path.replace(".h264", ".mp4");
        boolean isH264ToMp4Success = H264ToMp4Utils.h264ToMp4Parse(h264Path, mp4Path);
        return isH264ToMp4Success;
    }


    public void setStoreH264File(boolean storeH264File) {
        isStoreH264File = storeH264File;
    }

    public void setIh264StoreFinishCallBack(IH264StoreFinishCallBack ih264StoreFinishCallBack) {
        this.ih264StoreFinishCallBack = ih264StoreFinishCallBack;
    }

    /**
     * 添加数据到消息队列的最后面
     */
    public void addByteToQueue(byte[] bytes, String filePath) {
        this.h264CurrentFilePath = filePath;

        if (mH264BytesQueue != null) {
            if (bytes != null) {
                mH264BytesQueue.offer(bytes);
            }
        }
    }


    /**
     * 获取并且移除队列的头。如果队列为空，则为nul
     */
    public byte[] getFirstQueue() {
        if (mH264BytesQueue != null) {
            //returns {@code null} if this queue is empty
            return mH264BytesQueue.poll();
        }
        return null;
    }


    /**
     * 判断消息队列是否还有数据
     *
     * @return 队列是否为空
     */
    public boolean isQueueEmpty() {
        boolean result;
        if (mH264BytesQueue != null) {
            result = mH264BytesQueue.isEmpty();
        } else {
            result = true;
        }
        return result;
    }


    /**
     * 清空当前消息队列
     */
    public void clearQueue() {
        if (mH264BytesQueue != null) {
            mH264BytesQueue.clear();
            mH264BytesQueue = null;
        }
    }


    /**
     * 获取当前消息队列还有多少个数据未处理
     */
    public int getQueueSize() {
        if (mH264BytesQueue != null) {
            return mH264BytesQueue.size();
        }
        return 0;
    }

    /**
     * 结束线程 和 清空队列
     */
    public void stopThreadAndClearQueue() {
        if (mH264BytesQueue != null) {
            mH264BytesQueue.clear();
        }
        isStoreH264File = false;
    }

}
